package manager;

import controller.LeaveRemote;
import dtos.LeaveDTO;
import org.primefaces.component.dashboard.Dashboard;
import org.primefaces.component.panel.Panel;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;
import utils.UserDAORemote;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Logger;

@ManagedBean
@SessionScoped
public class DashboardBean {

    private Logger LOG = Logger.getLogger(DashboardBean.class.getName());

    @EJB
    private LeaveRemote leaveBean;

    @EJB
    private UserDAORemote userDAORemote;

    private Dashboard dashboard;

    private DashboardModel dashboardModel;
    /*private DashboardColumn leftCol;
    private DashboardColumn rightCol;
    private DashboardColumn middleCol;*/

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyyy-MM-dd");

    @PostConstruct
    public void init() {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application application = fc.getApplication();

        dashboard = (Dashboard) application.createComponent(fc, "org.primefaces.component.Dashboard", "org.primefaces.component.DashboardRenderer");
        dashboard.setId("dashboard");

        dashboardModel = new DefaultDashboardModel();

        DashboardColumn leftCol = new DefaultDashboardColumn();
        DashboardColumn rightCol = new DefaultDashboardColumn();
        DashboardColumn middleCol = new DefaultDashboardColumn();

        dashboardModel.addColumn(leftCol);
        dashboardModel.addColumn(middleCol);
        dashboardModel.addColumn(rightCol);

        dashboard.setModel(dashboardModel);

        List<LeaveDTO> leaves = leaveBean.getAllLeaves();

        for (LeaveDTO l : leaves) {
            Panel panel = (Panel) application.createComponent(fc, "org.primefaces.component.Panel", "org.primefaces.component.PanelRenderer");
            panel.setId("p" + Integer.toString(l.getId()));
            panel.setHeader(userDAORemote.getUser(l.getUser()).getName() + " [" + sdf.format(l.getFrom())
                    + " - " + sdf.format(l.getUntil()) + "]");
            panel.setToggleable(true);
            panel.setCollapsed(false);
            dashboard.getChildren().add(panel);

            HtmlOutputText text = new HtmlOutputText();
            text.setId("t" + panel.getId());
            text.setValue(leaveBean.getLeaveTypeById(l.getType()).getName() + " - " + l.getReason());

            panel.getChildren().add(text);

            if (l.getStatus() == 3) {
                leftCol.addWidget(panel.getId());
            } else if (l.getStatus() == 1) {
                middleCol.addWidget(panel.getId());
            } else {
                rightCol.addWidget(panel.getId());
            }

        }

    }


    public void reorder(DashboardReorderEvent event) {
        try {

            if (event.getSenderColumnIndex() != null) {

                int id = Integer.parseInt(event.getWidgetId().substring(1));

                LeaveDTO leave = leaveBean.getLeaveById(id);

                FacesMessage message = new FacesMessage();
                message.setSeverity(FacesMessage.SEVERITY_INFO);
                message.setSummary("Saved");
                if (event.getColumnIndex() == 0) {
                    //declined
                    message.setDetail("Request declined for ");
                    leaveBean.declineLeave(id);
                } else if (event.getColumnIndex() == 2) {
                    //approved
                    message.setDetail("Request approved for ");
                    leaveBean.approveLeave(id);
                } else {
                    //decision canceled
                    message.setDetail("Request decision canceled for ");
                    leaveBean.setUnprocessedLeave(id);
                }
                message.setDetail(message.getDetail().concat(userDAORemote.getUser(leave.getUser()).getName()));
                addMessage(message);
            }
        } catch (NumberFormatException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * *******************************************************************************
     */
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    /**
     * *******************************************************************************
     */

    public DashboardModel getDashboardModel() {
        return dashboardModel;
    }

    public void setDashboardModel(DashboardModel dashboardModel) {
        this.dashboardModel = dashboardModel;
    }

    public Dashboard getDashboard() {
        return dashboard;
    }

    public void setDashboard(Dashboard dashboard) {
        this.dashboard = dashboard;
    }
}
